﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Prism.Games.GalEngine.Scripts.FunctionModel
{
    public interface IItemIncrementalMarkable
    {
        void Add(object value);
        void Insert(int index, object value);
        void Insert(int index, object value, bool doNotSortId);
        void Remove(object value);
    }

    public interface IItemIncrementalMarkable<T>
    {
        void Add(T value);
        void Insert(int index, T value);
        void Insert(int index, T value, bool doNotSortId);
        void Remove(T value);
    }

    [Serializable]
    public class ItemIncrementalMarkedCollection<T> : ObservableCollection<T>, IItemIncrementalMarkable<T>, IItemIncrementalMarkable, ICounter
        where T : IId
    {
        public ItemIncrementalMarkedCollection()
        {
            this.Counter = -1;
        }

        public int Counter { get; set; }

        public new void Add(T value)
        {
            this.Counter++;
            value.Id = this.Counter;
            base.Add(value);
        }

        public new void Insert(int index, T value)
        {
            if (index < 0) return;
            this.Counter++;
            value.Id = this.Counter;
            base.Insert(index, value);
        }

        public void Insert(int index, T value, bool doNotSortId)
        {
            if (index < 0) return;
            if (doNotSortId)
            {
                if (index >= this.Count)
                {
                    base.Add(value);
                    return;
                }
                else
                {
                    base.Insert(index, value);
                    return;
                }
            }
            else
            {
                this.Insert(index, value);
                return;
            }
        }

        public new void Remove(T value)
        {
            base.Remove(value);
        }

        void IItemIncrementalMarkable.Add(object value)
        {
            this.Counter++;
            ((T)value).Id = this.Counter;
            base.Add((T)value);
        }

        void IItemIncrementalMarkable.Insert(int index, object value)
        {
            if (index < 0) return;
            this.Counter++;
            ((T)value).Id = this.Counter;
            base.Insert(index, (T)value);
        }

        void IItemIncrementalMarkable.Insert(int index, object value, bool doNotSortId)
        {
            if (index < 0) return;
            if (doNotSortId)
            {
                if (index >= this.Count)
                {
                    base.Add((T)value);
                    return;
                }
                else
                {
                    base.Insert(index, (T)value);
                    return;
                }
            }
            else
            {
                this.Insert(index, (T)value);
                return;
            }
        }

        void IItemIncrementalMarkable.Remove(object value)
        {
            base.Remove((T)value);
        }
    }
}
